package panel.setPanel
{
	import assists.HtmlText;
	import assists.TextFieldUtils;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import main.ClientConstants;
	
	public class SlideBtn extends Sprite
	{
		private var _slideBtn:Sprite = new Sprite();
		private var _tipTxt:TextField = new TextField();
		
		private var _backGround:Bitmap;
		private var _callBack:Function;
		private var _loader:ResourceLoader;
		private var _state:Boolean;
		public function SlideBtn(loader:ResourceLoader)
		{
			_loader = loader;
			init();
		}
		
		private function init():void
		{
			_backGround = _loader.uiManager.generateBmp(_loader.uiManager.slideBtnBack);
			var btn:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.slideBtn);
			_slideBtn.addChild(btn);
			_backGround.visible = true;
			_slideBtn.visible = false;
			
			_tipTxt.mouseEnabled = false;
			_tipTxt.autoSize = "left";
			TextFieldUtils.setHtmlText(_tipTxt,HtmlText.format("已关"));
			_tipTxt.x = _slideBtn.x + (_slideBtn.width - _tipTxt.width) * 0.5;
			
			_tipTxt.y = _slideBtn.y + _slideBtn.height;
			this.addChild(_backGround);
			this.addChild(_tipTxt);
			this.addChild(_slideBtn);
			this.addEventListener(MouseEvent.CLICK,slideHandler);
		}
		
		protected function slideHandler(event:MouseEvent):void
		{
			if(_state)
			{
				_backGround.visible = false;
				_slideBtn.visible  = true;
//				TweenLite.to(_slideBtn,0.2,{x:0});
				changeLabel(false);
			}else
			{
				
				changeLabel(true);
				_backGround.visible = true;
				_slideBtn.visible  = false;
//				TweenLite.to(_slideBtn,0.2,{x:(_backGround.width - _slideBtn.width)});
			}
			if(_state)
			{
				_callBack(ClientConstants.PIC_LOAD_NO);
			}else
			{
				_callBack(ClientConstants.PIC_LOAD_YES);	
			}
			_state = !_state;
		}
		
		public function show(state:Boolean,callBack:Function):void
		{
			_callBack = callBack;
			if(state)
			{
				_slideBtn.x = (_backGround.width - _slideBtn.width);
				changeLabel(true);
			}else
			{
				changeLabel(false);
			}
			_state = state;
		}
		
		private function changeLabel(value:Boolean):void
		{
			if(value)
			{
				TextFieldUtils.setHtmlText(_tipTxt,HtmlText.format("已开"));
//				_tipTxt.x = 0;
			}else
			{
				TextFieldUtils.setHtmlText(_tipTxt,HtmlText.format("已关"));
//				_tipTxt.x = _backGround.width - _tipTxt.width;
			}
		}
		
		
	}
}